package com.example.testnt;

import android.os.Handler;
import android.widget.Button;
import android.widget.TextView;

public abstract class AbstractEchoTask extends Thread {

    private final Handler handler;
    private Button startButton;
    private TextView logView;

    protected AbstractEchoTask( Button startButton, TextView logView) {
        this.handler = new Handler();
        this.startButton = startButton;
        this.logView = logView;
    }


    protected void onPreExecute() {

        startButton.setEnabled(false);
        logView.setText("");
    }

    public synchronized void start() {
        onPreExecute();
        super.start();
    }

    @Override
    public void run() {
        onBackground();
        handler.post(new Runnable() {
            @Override
            public void run() {
                onPostExecute();
            }
        });
    }

    protected abstract void onBackground();

    protected void onPostExecute() {

        startButton.setEnabled(true);
    }
}

